﻿namespace Er.Shared.ViewModels
{
    using System;
    using System.Windows.Input;

    using Microsoft.Practices.Composite.Presentation.Commands;

    internal class WordEditorViewModel : IWordEditorViewModel
    {
        private readonly IWordViewModelFactory _wordViewModelFactory;

        private readonly IWordViewModelRepository _wordViewModelRepository;

        private IWordViewModel _newWord;

        private ICommand _addWordCommand;

        private ICommand _clearWordCommand;

        public WordEditorViewModel(
            IWordViewModelFactory wordViewModelFactory, IWordViewModelRepository wordViewModelRepository)
        {
            _wordViewModelFactory = wordViewModelFactory;
            _wordViewModelRepository = wordViewModelRepository;

            _newWord = _wordViewModelFactory.Create();

            _addWordCommand = new DelegateCommand(() => _wordViewModelRepository.Add(_newWord));
            _clearWordCommand = new DelegateCommand(
                () =>
                    {
                        _newWord.Id = Guid.NewGuid();
                        _newWord.Spell = string.Empty;
                    });
        }

        public IWordViewModel NewWord
        {
            get
            {
                return _newWord;
            }
        }

        public ICommand AddWordCommand
        {
            get
            {
                return _addWordCommand;
            }
        }

        public ICommand ClearWordCommand
        {
            get
            {
                return _clearWordCommand;
            }
        }
    }
}